package com.pactera.asmp.server.rabbit.listener;

import com.alibaba.fastjson.JSON;
import com.pactera.asmp.server.common.constants.Constants;
import com.pactera.asmp.server.entity.PluginConfigMqDto;
import com.pactera.asmp.server.pojo.TPluginConfig;
import com.pactera.asmp.server.service.IPluginConfigService;
import com.pactera.asmp.server.utils.CacheBeanCopier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.service
 * @ClassName: PluginConfigRabbitListener
 * @Description: PluginConfigRabbitListener
 * @Date 2020/07/24 16:34
 * @Author lvzhiqiang
 * @Version 1.0
 */
@Component
@RabbitListener(queues = {Constants.PLUGIN_CONFIG_APK_RESULT_QUEUE})
@Slf4j
public class PluginConfigRabbitListener {
    @Autowired
    private IPluginConfigService pluginConfigService;

    @RabbitHandler
    public void process(String msg) {
        log.info("PluginConfigRabbitListener process received msg: {}", msg);
        try {
            PluginConfigMqDto pluginConfigMqDto = JSON.parseObject(msg, PluginConfigMqDto.class);
            if (null == pluginConfigMqDto || null == pluginConfigMqDto.getId()
                    || null == pluginConfigMqDto.getStatus()) {
                return;
            }
            TPluginConfig pluginConfig = new TPluginConfig();
            CacheBeanCopier.copy(pluginConfigMqDto, pluginConfig);
            pluginConfig.setUpdateTime(new Date());
            pluginConfigService.update(pluginConfig);
        } catch (Exception e) {
            log.error("PluginConfigRabbitListener process error: ", e);
        }
    }
}
